package com.love.marriage.userChatInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.userChatInfo.entity.UserChatInfo;
import com.love.marriage.userChatInfo.mapper.UserChatInfoMapper;
import com.love.marriage.userChatInfo.service.UserChatInfoService;
import com.love.marriage.userChatInfo.vo.UserChatInfoVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  用户聊天记录表服务实现类
 * author:       mybatisPlus
 * createDate:   2024-10-20 01:13:08
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class UserChatInfoServiceImp extends ServiceImpl<UserChatInfoMapper, UserChatInfo> implements UserChatInfoService {

    private final UserChatInfoMapper userChatInfoMapper;

    @Override
    public Page<UserChatInfoVo> getPage(Long pageNum, Long pageSize, UserChatInfoVo userChatInfoVo) {
        Page<UserChatInfoVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return userChatInfoMapper.getPage(page, userChatInfoVo);
    }

    @Override
    public UserChatInfoVo queryUserChatInfo(Long id) {
        return userChatInfoMapper.queryUserChatInfo(id);
    }

    @Override
    public UserChatInfoVo queryUserChatInfoByUserId(Long userId, String type) {
        return userChatInfoMapper.queryUserChatInfoByUserId(userId, type);
    }

    @Override
    public UserChatInfoVo addUserChatInfo(UserChatInfoVo userChatInfoVo) {
        UserChatInfo userChatInfo = new UserChatInfo();
        BeanUtils.copyProperties(userChatInfoVo, userChatInfo);
        userChatInfoMapper.insert(userChatInfo);
        UserChatInfoVo res = new UserChatInfoVo();
        BeanUtils.copyProperties(userChatInfo, res);
        return res;
    }

    @Override
    public UserChatInfoVo addInfo(UserChatInfoVo userChatInfoVo) {
        UserChatInfoVo dbInfo = queryUserChatInfoByUserId(userChatInfoVo.getUserId(), userChatInfoVo.getType());
        if (dbInfo == null) {
            return addUserChatInfo(userChatInfoVo);
        } else {
            userChatInfoVo.setId(dbInfo.getId());
            return updateUserChatInfo(userChatInfoVo);
        }
    }

    @Override
    public UserChatInfoVo updateUserChatInfo(UserChatInfoVo userChatInfoVo) {
        UserChatInfo userChatInfo = new UserChatInfo();
        BeanUtils.copyProperties(userChatInfoVo, userChatInfo);
        userChatInfoMapper.updateById(userChatInfo);
        return userChatInfoVo;
    }

    @Override
    public Boolean deleteUserChatInfo(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        userChatInfoMapper.deleteBatchIds(idArr);
        return true;
    }
}
